package cn.edu.hit.selflearning;

import cn.edu.hit.core.Document;
import cn.edu.hit.core.db.*;
import cn.edu.hit.kg.data.GraphData;

import java.util.ArrayList;
import java.util.List;
import java.util.stream.Collectors;

public class NewGetPort {
    static DataConnection connection = DataConnectionLoader.getInstance();
    static DataReader reader = connection.getReader();
    static DBReadHelper dbHelper = new DBReadHelper(reader);
    static GraphData graphData = GraphData.getInstance();

    public static void main(String[] args) {

        List<Document> signalList = dbHelper.loadConfigure("type", "signal");

        System.out.println(signalList.get(0).get("_id").toString());
//        signalList.stream().map(signal ->{
//            signal.
//        })
//
//
//        Long s =new Long(123);
//        getFaultPort(s);
    }

    public static List<String> getFaultPort(Long faultId) {
        List<String> featureList = graphData.getAllSubNodes(faultId)
                .filter(node -> node.hasLabel("Feature"))
                .map(node -> (String) node.asMap().get("name"))
                .collect(Collectors.toList());

        List<String> portNames = new ArrayList<>();
        for (int ij = 0; ij < featureList.size(); ij++) {
            String featureName = featureList.get(ij);
            List<Document> channelList = reader.queryByTemplate(Filters.eq("signalType", featureName));
            for (int j = 0; j < channelList.size(); j++) {
                String channelIdStr = channelList.get(j).getString("CombineChanenels");
                String[] IdArray = channelIdStr.substring(1, channelIdStr.length() - 1).split(",");
                for (int k = 0; k < IdArray.length; k++) {
                    String portId = IdArray[k];
                    if (portId.length() > 5) {
                        Document portDocunment = dbHelper.getById(portId.substring(1, portId.length() - 1));
                        if (portDocunment != null) {
                            String dataSourceArgs = portDocunment.getString("dataSourceArgs");
                            String portName = portDocunment.getString("portName");
                            boolean hasport = false;
                            for (int l = 0; l < portNames.size(); l++) {
                                if (portNames.get(l).equals(portName)) {
                                    hasport = true;
                                }
                            }
                            if (!hasport) {
                                portNames.add(portName);
                            }
                        }
                    }
                }
            }
        }
        return portNames;
    }


}
